<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\BannerModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\NetworkPointsModel;
use App\Models\yb\NoticeModel;
use App\Models\yb\UserModel;
use App\Models\yb\UserAddressModel;
use App\Models\yb\SettingModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class IndexController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
//    public function __construct()
//    {
//        $this->middleware('auth:api', ['except' => ['preview']]);
//    }

    public function MapNetwork(Request $request){

        $latitude = $request->post('latitude', 0);   //纬度

        $longitude = $request->post('longitude', 0); //经度

        $openid = $request->post('openid');

        $network_point_data = NetworkPointsModel::where(['status'=>1])->get();

        $point=[
            'lng'=>$longitude,
            'lat'=>$latitude
        ];

        $data = [];

        foreach($network_point_data as $key=>$val){
            if(!empty($val['points'])){
                $points = [];
                foreach(json_decode($val['points'],true) as $k=>$v){
                    $points[$k]['lng'] = $v['latLng']['lng'];
                    $points[$k]['lat'] = $v['latLng']['lat'];
                }
                $is_ptin_poly = AdminUserModel::is_point_in_polygon($point,$points);
                if($is_ptin_poly){
                    $data = AdminUserModel::where(['id'=>$val['admin_id']])->first();
                    break;
                }
            }
        }

//        $data = AdminUserModel::selectRaw('*,(st_distance(point(longitude,latitude),point(?,?))/0.0111) AS distance',[$longitude,$latitude])->where(['type'=>2])->orderBy('distance','asc')->first();

        $address_data = array();

        if(!empty($openid)){
            $user_data = UserModel::where(['openid'=>$openid])->first();
            $address_data = UserAddressModel::where(['user_id'=>$user_data['id'],'is_default'=>1])->first();
        }

        $list['data'] = $data;
        $list['address_data'] = $address_data;

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);

    }

    /**
     * Get a JWT via given credentials.
     *  首页轮播图+公告
     * @return \Illuminate\Http\JsonResponse
     */
    public function BannerListing(){

        $banner_list = BannerModel::select(['id','title','banner'])->orderBy('created_at','desc')->get();

        $setting_data = SettingModel::select('customer_phone')->first();

        foreach($banner_list as $key=>$val){
            $banner_list[$key]['banner'] = env('OSS_URL').$val['banner'];
        }
        $notice_list = NoticeModel::select(['id','title','content'])->first();

        $data['banner_list'] = $banner_list;

        $data['notice_list'] = $notice_list;

        $data['customer_phone'] = isset($setting_data['customer_phone'])?$setting_data['customer_phone']:"";

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);
    }


    /**
     * Get a JWT via given credentials.
     * 商品列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function GoodsListing(Request $request){

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $type_id = $request->post('type_id');

        $title = $request->post('title');

        $network_id = $request->post('network_id');

        $network_goods = NetworkGoodsModel::where(['network_id'=>$network_id,'status'=>1])->where('goods_stock','>',0)
                        ->when($title,function($query,$title){
                            return  $query->where('goods_title','like',"%".$title."%");
                        })->when($type_id,function($query,$type_id){
                            return  $query->where(['goods_type'=>$type_id]);
                        })->orderBy('weight','desc')
                        ->skip(($page - 1) * $take)
                        ->take($take)
                        ->get();

        $data = array();

        foreach($network_goods as $key=>$val){

            $goods_data = GoodsModel::where(['id'=>$val['goods_id'],'status'=>1])->first();
            if(empty($goods_data) || $goods_data['is_client_show'] != 1){
                continue;
            }
            $goods_data['goods_cover'] = env('OSS_URL').$goods_data['goods_cover'];
            $goods_data['goods_type_name'] = GoodsTypeModel::selectValue('id',$goods_data['goods_type']);
            $goods_data['network_goods_id'] = $val['id'];
            if($val['is_fictitious'] == 1){
                $goods_data['goods_sales'] = $val['fictitious_goods_sales'];
            }else{
                $goods_data['goods_sales'] = $val['goods_sales'];
            }
            $data[] = $goods_data;

        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     * 轮播图详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function BannerDetail(Request $request){

        $id = $request->post('id');

        $banner_list = BannerModel::where(['id'=>$id])->select(['id','title','banner','content'])->first();

        $banner_list['banner'] = env('OSS_URL').$banner_list['banner'];

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$banner_list]);

    }
    /**
     * Get a JWT via given credentials.
     * 公告详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeDetail(Request $request){
        $id = $request->post('id');

        $banner_list = NoticeModel::where(['id'=>$id])->select(['id','title','content'])->first();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$banner_list]);

    }

}
